package com.hongdee.crm.Template.service.impl;

import com.hongdee.common.dao.IHibernateDao;
import com.hongdee.crm.Template.dao.ITcrmTemplateDao;
import com.hongdee.crm.Template.entity.TCrmTemplateRef;
import com.hongdee.crm.Template.service.ITCrmTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dell on 2017/5/4.
 */
@Service
public class TCrmTemplateServiceImpl implements ITCrmTemplateService{
    @Autowired
    private ITcrmTemplateDao iTcrmTemplateDao;
    private  String tabName="t_crm_car_template";
    @Autowired
    private IHibernateDao iHibernateDao;

/*
* 停用启用
* */
    @Override
    public void update(String id, String templateStatus) throws Exception {
        String hql = "update TCrmTemplateRef set templateStatus = :templateStatus where id = :id ";
        Map params = new HashMap();
        params.put("id", id);
        params.put("templateStatus", templateStatus);
        this.iTcrmTemplateDao.update(hql, params);
    }

    @Override

    public TCrmTemplateRef saveOrUpdate(TCrmTemplateRef tem) throws Exception {
        //根据字典分组、字典存储值去重
        if (null == tem.getId() || StringUtils.isEmpty(tem.getId())) {
            //新增时判断是否已经存在
            if (isExist(tem.getTemplateNum(), tem.getTemplateName(),0,null)){

                throw new Exception("模板编码或模板名称已存在");
            }
            tem.setTemplateStatus("0");
            this.iTcrmTemplateDao.saveOrUpdate(tem);
        } else {
            if (isExist( tem.getTemplateNum(), tem.getTemplateName(),1, tem.getId())) {
                throw new Exception("模板编码或模板名称已存在");
            }
//            Map oldMap = this.iTcrmTemplateDao.info(tabName, "id", ref.getId());
            this.iTcrmTemplateDao.saveOrUpdate(tem);
        }
        //修正下一级编码
        return  tem;
    }







//判断模板编码，模板名称是否重复
private boolean isExist(String templateNum,String templateName ,int type,String id) throws Exception {
    String sql = "select * from " + tabName + " where ( template_num= ? or template_name = ? ) ";
    List param = new ArrayList();
    param.add(templateNum);
    param.add(templateName);
    //模块编码和模块名称不可以重复
    if (1 == type) {
        sql += " and  id != ? ";
        param.add(id);
    }
    Map map = this.iTcrmTemplateDao.page(sql, param.toArray(), null, null);
    return Integer.valueOf(map.get("total").toString()) > 0;
}
/*
* 根据条件查询
* */
@Override
public Map info(String id) throws Exception {
    String sql = "select * from " + tabName + " where id = ? ";
    return this.iTcrmTemplateDao.info(sql, id);
}
/*
* 分页
* **/
@Override
    public Map newPage(String start, String limit, String templateNum) throws Exception{
        String sql = "select * from t_crm_car_template   ";
        List<Object> params = new ArrayList();
       if (!StringUtils.isEmpty(templateNum)) {
        sql += " where template_name like ? or template_num like ? ";
        params.add("%" + templateNum + "%");
        params.add("%" + templateNum + "%");

    }
      return this.iTcrmTemplateDao.page(sql,params.toArray(),start,limit);
}
}
